home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-STLport.exe / {app} / Samples / common / src / CEGuiOpenGLBaseApplication.cpp < prev    next >
C/C++ Source or Header  |  2005-11-23  |  10KB  |  323 lines

  1. /************************************************************************
  2.     filename:   CEGuiOpenGLBaseApplication.cpp
  3.     created:    24/9/2004
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #   include "config.h"
  26. #endif
  27.  
  28. // this controls conditional compile of file for MSVC
  29. #include "CEGUIConfig.h"
  30. #ifdef CEGUI_SAMPLES_USE_OPENGL
  31.  
  32. #include "CEGuiOpenGLBaseApplication.h"
  33. #include "renderers/OpenGLGUIRenderer/openglrenderer.h"
  34. #include "CEGuiSample.h"
  35. #include "CEGUI.h"
  36.  
  37. #ifdef __APPLE__
  38. #include <GLUT/glut.h>
  39. #include <OpenGL/gl.h>
  40. #include <OpenGL/glu.h>
  41. #else
  42. #include <GL/glut.h>
  43. #include <GL/gl.h>
  44. #include <GL/glu.h>
  45. #endif
  46.  
  47. #include <stdexcept>
  48. #include <stdlib.h>
  49.  
  50. #ifdef _MSC_VER
  51. # if defined(DEBUG) || defined (_DEBUG)
  52. #   pragma comment (lib, "OpenGLGUIRenderer_d.lib")
  53. # else
  54. #   pragma comment (lib, "OpenGLGUIRenderer.lib")
  55. # endif
  56. #endif
  57.  
  58.  
  59. /*************************************************************************
  60.     Simple key-map used to translate glut special keys to CEGUI keys.
  61.     See: CEGuiOpenGLBaseApplication::keySpecial method.
  62. *************************************************************************/
  63. struct GlutKeyMapping
  64. {
  65.     int glutKey;
  66.     CEGUI::uint  ceguiKey;
  67. };
  68.  
  69. GlutKeyMapping specialKeyMap[] =
  70. {
  71.     {GLUT_KEY_F1, CEGUI::Key::F1},
  72.     {GLUT_KEY_F2, CEGUI::Key::F2},
  73.     {GLUT_KEY_F3, CEGUI::Key::F3},
  74.     {GLUT_KEY_F4, CEGUI::Key::F4},
  75.     {GLUT_KEY_F5, CEGUI::Key::F5},
  76.     {GLUT_KEY_F6, CEGUI::Key::F6},
  77.     {GLUT_KEY_F7, CEGUI::Key::F7},
  78.     {GLUT_KEY_F8, CEGUI::Key::F8},
  79.     {GLUT_KEY_F9, CEGUI::Key::F9},
  80.     {GLUT_KEY_F10, CEGUI::Key::F10},
  81.     {GLUT_KEY_F11, CEGUI::Key::F11},
  82.     {GLUT_KEY_F12, CEGUI::Key::F12},
  83.     {GLUT_KEY_LEFT, CEGUI::Key::ArrowLeft},
  84.     {GLUT_KEY_UP, CEGUI::Key::ArrowUp},
  85.     {GLUT_KEY_RIGHT, CEGUI::Key::ArrowRight},
  86.     {GLUT_KEY_DOWN, CEGUI::Key::ArrowDown},
  87.     {GLUT_KEY_PAGE_UP, CEGUI::Key::PageUp},
  88.     {GLUT_KEY_PAGE_DOWN, CEGUI::Key::PageDown},
  89.     {GLUT_KEY_HOME, CEGUI::Key::Home},
  90.     {GLUT_KEY_END, CEGUI::Key::End},
  91.     {GLUT_KEY_INSERT, CEGUI::Key::Insert},
  92.     {-1, 0}
  93. };
  94.  
  95. /*************************************************************************
  96.     Static Data
  97. *************************************************************************/
  98. bool CEGuiOpenGLBaseApplication::d_quitFlag = false;
  99. int  CEGuiOpenGLBaseApplication::d_lastFrameTime = 0;
  100.  
  101. /*************************************************************************
  102.     Constructor.
  103. *************************************************************************/
  104. CEGuiOpenGLBaseApplication::CEGuiOpenGLBaseApplication()
  105. {
  106.     // fake args for glutInit
  107.     int argc = 1;
  108.     char* argv = "SampleApp";
  109.  
  110.     // Do GLUT init
  111.     glutInit(&argc, &argv);
  112.     glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
  113.     glutInitWindowSize(800, 600);
  114.     glutInitWindowPosition(100, 100);
  115.     glutCreateWindow("Crazy Eddie's GUI Mk-2 - Sample Application");
  116.     glutSetCursor(GLUT_CURSOR_NONE);
  117.  
  118.     glutDisplayFunc(&CEGuiOpenGLBaseApplication::drawFrame);
  119.     glutReshapeFunc(&CEGuiOpenGLBaseApplication::reshape);
  120.     glutMotionFunc(&CEGuiOpenGLBaseApplication::mouseMotion);
  121.     glutPassiveMotionFunc(&CEGuiOpenGLBaseApplication::mouseMotion);
  122.     glutMouseFunc(&CEGuiOpenGLBaseApplication::mouseButton);
  123.     glutKeyboardFunc(&CEGuiOpenGLBaseApplication::keyChar);
  124.     glutSpecialFunc(&CEGuiOpenGLBaseApplication::keySpecial);
  125.  
  126.     // Set the clear color
  127.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  128.  
  129.     d_renderer = new CEGUI::OpenGLRenderer(1024);
  130.     new CEGUI::System(d_renderer);
  131. }
  132.  
  133.  
  134. /*************************************************************************
  135.     Destructor.
  136. *************************************************************************/
  137. CEGuiOpenGLBaseApplication::~CEGuiOpenGLBaseApplication()
  138. {
  139.     delete CEGUI::System::getSingletonPtr();
  140.     delete d_renderer;
  141. }
  142.  
  143.  
  144. /*************************************************************************
  145.     Start the base application
  146. *************************************************************************/
  147. bool CEGuiOpenGLBaseApplication::execute(CEGuiSample* sampleApp)
  148. {
  149.     sampleApp->initialiseSample();
  150.  
  151.     // set starting time
  152.     d_lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
  153.  
  154.     glutMainLoop();
  155.  
  156.     return true;
  157. }
  158.  
  159.  
  160. /*************************************************************************
  161.     Performs any required cleanup of the base application system.
  162. *************************************************************************/
  163. void CEGuiOpenGLBaseApplication::cleanup()
  164. {
  165.     // nothing to do here.
  166. }
  167.  
  168. /*************************************************************************
  169.     Set whether the app should quit
  170. *************************************************************************/
  171. void CEGuiOpenGLBaseApplication::setQuitting(bool quit)
  172. {
  173.     d_quitFlag = quit;
  174. }
  175.  
  176. /*************************************************************************
  177.     Is this app quitting
  178. *************************************************************************/
  179. bool CEGuiOpenGLBaseApplication::isQuitting() const
  180. {
  181.     return d_quitFlag;
  182. }
  183.  
  184. /*************************************************************************
  185.     Does whatever is required in one single frame
  186. *************************************************************************/
  187. void CEGuiOpenGLBaseApplication::drawFrame(void)
  188. {
  189.     // do time based updates
  190.     int thisTime = glutGet(GLUT_ELAPSED_TIME);
  191.     float elapsed = static_cast<float>(thisTime - d_lastFrameTime);
  192.     d_lastFrameTime = thisTime;
  193.     // inject the time pulse
  194.     CEGUI::System::getSingleton().injectTimePulse(elapsed / 1000.0f);
  195.  
  196.     // do rendering for this frame.
  197.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  198.     glEnable(GL_DEPTH_TEST);
  199.     glMatrixMode(GL_MODELVIEW);
  200.     glLoadIdentity();
  201.     gluLookAt(0.0, 0.0, 12, 0.0, 0.0, -100, 0.0, 1.0, 0.0);
  202.  
  203.     CEGUI::System::getSingleton().renderGUI();
  204.  
  205.     glFlush();
  206.     glutPostRedisplay();
  207.     glutSwapBuffers();
  208.  
  209.     // here we check the 'quitting' state and cleanup as required.
  210.     // this is probably not the best way to do this, but since we're
  211.     // using glut, and glutMainLoop can never return, we need some
  212.     // way of checking when to exit.  And this is it...
  213.     if (d_quitFlag)
  214.     {
  215.         // cleanup cegui system
  216.         CEGUI::Renderer* renderer = CEGUI::System::getSingleton().getRenderer();
  217.         delete CEGUI::System::getSingletonPtr();
  218.         delete renderer;
  219.  
  220.         // exit
  221.         exit(0);
  222.     }
  223. }
  224.  
  225. void CEGuiOpenGLBaseApplication::reshape(int w, int h)
  226. {
  227.     glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  228.     glMatrixMode (GL_PROJECTION);
  229.     glLoadIdentity ();
  230.     gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 50.0);
  231.     glMatrixMode(GL_MODELVIEW);
  232. }
  233.  
  234. void CEGuiOpenGLBaseApplication::mouseMotion(int x, int y)
  235. {
  236.     CEGUI::System::getSingleton().injectMousePosition(x, y);
  237. }
  238.  
  239. void CEGuiOpenGLBaseApplication::mouseButton(int button, int state, int x, int y)
  240. {
  241.     switch(button)
  242.     {
  243.     case  GLUT_LEFT_BUTTON:
  244.         if (state == GLUT_UP)
  245.         {
  246.             CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
  247.         }
  248.         else
  249.         {
  250.             CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
  251.  
  252.         }
  253.         break;
  254.  
  255.     case GLUT_RIGHT_BUTTON:
  256.         if (state == GLUT_UP)
  257.         {
  258.             CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
  259.         }
  260.         else
  261.         {
  262.             CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
  263.         }
  264.         break;
  265.  
  266.     case GLUT_MIDDLE_BUTTON:
  267.         if (state == GLUT_UP)
  268.         {
  269.             CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
  270.         }
  271.         else
  272.         {
  273.             CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
  274.         }
  275.         break;
  276.     }
  277.  
  278. }
  279.  
  280. void CEGuiOpenGLBaseApplication::keyChar(unsigned char key, int x, int y)
  281. {
  282.     // extract some keys may be handled via key code and generate those too
  283.     switch (key)
  284.     {
  285.     case 0x08:  // backspace
  286.         CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::Backspace);
  287.         break;
  288.     case 0x7F:  // delete
  289.         CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::Delete);
  290.         break;
  291.     case 0x1B:  // Escape
  292.         d_quitFlag = true;
  293.         break;
  294.     case 0x0D:  // CR (Return)
  295.         CEGUI::System::getSingleton().injectKeyDown(CEGUI::Key::Return);
  296.         break;
  297.     default:
  298.         // inject Character code
  299.         CEGUI::System::getSingleton().injectChar(static_cast<CEGUI::utf32>(key));
  300.         break;
  301.     }
  302.  
  303. }
  304.  
  305. void CEGuiOpenGLBaseApplication::keySpecial(int key, int x, int y)
  306. {
  307.     GlutKeyMapping* mapping = specialKeyMap;
  308.  
  309.     while (mapping->glutKey != -1)
  310.     {
  311.         if (mapping->glutKey == key)
  312.         {
  313.             CEGUI::System::getSingleton().injectKeyDown(mapping->ceguiKey);
  314.             return;
  315.         }
  316.  
  317.         ++mapping;
  318.     }
  319. }
  320.  
  321. #endif
  322.  
  323.